

/**
 * 单链表:
 *  1. 添加元素
 *  2. 删除元素
 *  3. 查找元素
 *  4. 大小
 */
public class Mylink {
    /**
     * 头指针
     */
    private Node head;

    /**
     * 元素数量
     */
    private int size;

    /**
     * 内部类定义了一个节点，每个节点存了一个自己本身的具体值和下一个节点地址
     */
    public class Node {
        private Object value;  //当前元素
        private Node next;   //节点的后一个元素的地址
        public Node(Object value,Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 添加元素
     * @param element
     */
    public void add(Object element) {
        if (null == element) {
            throw new NullPointerException("不能存null");
        }
        Node current = new Node(element,null);
        if (head == null) {   //如果头指针等于null
            head = current;   //那么current就是头指针了
        }else {
            Node node = head;
            //遍历找到最后一个元素
            while(node.next != null) {
                node = node.next;  //循环遍历直到找到next为null
            }
            node.next = current;
        }
        size++;
    }
    /**
     * 删除元素
     */
    public Object delete(int index) {
        if (index > size-1 || index < 0) {
            throw new RuntimeException("下标异常");
        }
        Node object = null;
        if (index == 0) {
            object = head;
            head = head.next; //删除下标为1的元素
        }else {
            Node pre = head;  //被删的前一个元素
            Node node = head.next;  //被删除的元素
            for (int i = 1; i < index; i++) {
                node = pre;
                node = node.next;
            }
            object = node;
            pre.next = node.next;
        }
        size--;
        return object.value;

    }
    public Object remove(Object element) {
        //被删除的元素
        Node node = null;
        if (head.value.equals(element)) {
            node = head;
            head = head.next;  //删除
            size -- ;
            return node.value;
        }else {
            Node pre = head;   //被删的前一个元素
            node = head.next;   //被删除的元素
            while(node != null) {
                if (node.value.equals(element)) {
                    pre.next = node.next;
                    size--;
                    return node.value;
                }
                else {
                    pre = node;
                    node = node.next;
                }
            }
        }
        return null;
    }

    /**
     * 获取元素
     * @param index
     * @return
     */
    public Object get(int index) {
        if (index < 0 || index > size) {
            throw new RuntimeException("下标不合法");
        } else if (index == 0) {
            return head.value;
        }else {
            Node node = head.next;
            for (int i = 1; i < index; i++) {
                node = node.next;
            }
            return node.value;
        }
    }
    /**
     * 大小
     */
    public int size() {
        return this.size;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Mylink【");
        Node node = head;
        while (node != null) {
            sb.append(node.value + ", ");
            node = node.next;
        }
        return sb.substring(0,sb.length()-2) + "】";
    }
}
